using System;
using System.Data;
using System.Data.SqlClient;
using Kugar.Core.Data.MSSQLHelper;
using Kugar.Core.ExtMethod;

namespace Kugar.Core.Security.Providers.MSSQL
{
    public class MSSQL_UserGroupManager : IDisposable, IUserGroupManager
    {
        
        private Func<SQLDataHelper> _connGetter;
        private bool _isAutoDisposeConn = false;

        public MSSQL_UserGroupManager(Func<SQLDataHelper> connGetter, bool isAutoDisposeConn = false)
        {
            _connGetter = connGetter;
            _isAutoDisposeConn = isAutoDisposeConn;
        }

        public bool UserGroup_Insert(RMS_UserGroup groupInfo)
        {
            var conn = GetConnection();

            var cmd = conn.CreateCommand();
            cmd.CommandText = "p_UserGroup_Insert";
            cmd.Parameters.AddRange(conn.GetProcedureParameters(cmd.CommandText));

            cmd.Parameters.SetValue(groupInfo);

            //cmd.Parameters.SetValue("@GroupID",groupInfo.GroupID);
            //cmd.Parameters.SetValue("@GroupName", groupInfo.GroupName);
            //cmd.Parameters.SetValue("@Comment", groupInfo.Comment);

            try
            {
                return conn.ExecuteNoQuery(cmd) > 0;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (_isAutoDisposeConn)
                {
                    conn.Dispose();
                }
            }
        }

        public RMS_UserGroup UserGroup_GetPermissionByID(string groupID)
        {
            RMS_PermissionValueCollection collection = null;

            var conn = GetConnection();

            var cmd = (SqlCommand)conn.CreateCommand();

            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "";
            cmd.Parameters.AddWithValue("@GroupID", groupID);

            try
            {
                var group = conn.ExecuteDataEntity<RMS_UserGroup>(cmd, null, null, false);

                //collection = new RMS_PermissionValueCollection(lst);

                //return collection;

                //return group;
                return null;
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                if (_isAutoDisposeConn)
                {
                    conn.Dispose();
                }
            }


        }

        public bool UserGroup_UpdatePermission(int groupID, RMS_PermissionItemValue[] permissionList)
        {
            var conn = GetConnection();

            bool isSuccess = true;

            conn.BeginTransaction();

            conn.ExecuteProcedureNoQuery("p_PermissionGroup_PermissionClear", false, new[] { new SqlParameter("@GroupID", groupID), });

            var cmd = conn.CreateCommand();
            cmd.CommandText = "[p_UserGroup_InsertPermission]";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddRange(conn.GetProcedureParameters(cmd.CommandText));

            for (int i = 0; i < permissionList.Length; i++)
            {
                cmd.Parameters.SetValue("@GroupID", groupID);
                cmd.Parameters.SetValue("@PermissionID", permissionList[i].PermissionID);
                cmd.Parameters.SetValue("@ControlValue", permissionList[i].ControlValue);

                try
                {
                    conn.ExecuteNoQuery(cmd);
                }
                catch (Exception)
                {
                    isSuccess = false;
                    conn.RollbackTransaction();
                    break;
                }
            }

            if (isSuccess)
            {
                conn.CommitTransaction();
            }

            if (_isAutoDisposeConn)
            {
                conn.Dispose();
            }

            return isSuccess;



        }

        public void UserGroup_DeleteByID(int groupID)
        {
            var conn = GetConnection();

            try
            {
                conn.ExecuteProcedureNoQuery("p_PermissionGroup_Delete", false,
                                             new[] {new SqlParameter("@GroupID", groupID),});
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (_isAutoDisposeConn)
                {
                    conn.Dispose();
                }
            }
            

            

        }

        public bool UserGroup_AddUser(int groupID, int userID)
        {
            var conn = GetConnection();

            try
            {
                return conn.ExecuteProcedureNoQuery("p_UserGroup_AddUser", false, new[]
                    {
                        new SqlParameter("@GroupID", groupID),
                        new SqlParameter("@UserID", userID),
                    }) > 0;
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                if (_isAutoDisposeConn)
                {
                    conn.Dispose();
                }
            }



        }

        public bool UserGroup_DeleteUser(int groupID, int userID)
        {
            var conn = GetConnection();

            try
            {
                return conn.ExecuteProcedureNoQuery("p_UserGroup_DeleteUser", false, new[]
                    {
                        new SqlParameter("@UserID", userID)
                    }) > 0;
            }
            catch (Exception ex)
            {
                Kugar.Core.Log.LoggerManager.CoreLogger.Error(ex.Message);
                throw;
            }
            finally
            {
                if (_isAutoDisposeConn)
                {
                    conn.Dispose();
                }
            }


        }

        public RMS_UserGroup[] ToArray()
        {
            return null;
        }

        #region Implementation of IDisposable

        public void Dispose()
        {
            //_pool = null;
        }

        #endregion


        private SQLDataHelper GetConnection()
        {
            return _connGetter();
        }
    }
}